﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 自动关机
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            
            InitializeComponent();
        }

        /// <summary>
        /// 关机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            int seconds = Convert.ToInt32(numericUpDown1.Value);

            Thread thread = new Thread(() =>
            {
                for (int i = seconds; i > 0; i--)
                {
                    Thread.Sleep(1000);
                    UpdateLabelText(i.ToString());
                }

                Process.Start("shutdown", " /s /t 0");

            });
            thread.Start();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// 更新label的显示内容
        /// </summary>
        /// <param name="text"></param>
        private void UpdateLabelText(string text)
        {
            if(label1.InvokeRequired)
            {
                label1.Invoke(new Action(() => { label1.Text = text; }));
            }
            else
            {
                label1.Text = text;
            }
        }

        /// <summary>
        /// 重启
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            int seconds = Convert.ToInt32(numericUpDown1.Value);
            Thread thread = new Thread(() =>
            {
                for (int i = seconds; i > 0; i--)
                {
                    Thread.Sleep(1000);
                    UpdateLabelText(i.ToString());
                }

                Process.Start("shutdown", " /r /t 0");

            });
            thread.Start();
        }


        /// <summary>
        /// 睡眠
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            int seconds = Convert.ToInt32(numericUpDown1.Value);
            Thread thread = new Thread(() =>
            {
                for (int i = seconds; i > 0; i--)
                {
                    Thread.Sleep(1000);
                    UpdateLabelText(i.ToString());
                }

                Process.Start("shutdown", " /h /t 0");

            });
            thread.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            numericUpDown1.Value = 5;
        }
    }
}
